//
//  PassWordView.m
//  lhpc
//
//  Created by 李英杰 on 2017/9/4.
//  Copyright © 2017年 lhpc. All rights reserved.
//

#import "PassWordView.h"
#import "PasswordInputView.h"

@interface PassWordView ()<PasswordInputViewDelegate>

@property (nonatomic, strong) UIView * backView;

@property (nonatomic, strong) UIView * headView;
@property (nonatomic, strong) UILabel * titleLabel;
@property (nonatomic, strong) UIButton * closeButton;


@property (nonatomic, strong) UIView *mainView;
@property (nonatomic, strong) PasswordInputView * inputView;

@property (nonatomic, strong) UIButton * forgetPassButton;

@end


@implementation PassWordView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupChildViews];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

-(void)passwordHasInPut:(NSString *)passwordStr{
    
    if (self.completeInPutPassWord) {
        self.completeInPutPassWord(passwordStr);
    }
    
}

-(void)closeButtonAction{
    
    if (self.abordInPutPassWord) {
        self.abordInPutPassWord();
    }
}

-(void)forgetPassWordAction{
    
    if (self.frogetPassWordBlock) {
        self.frogetPassWordBlock();
    }
}


-(void)setTitle:(NSString *)title{
    _title = title;
    
    self.titleLabel.text = title;
    
}

-(void)setupChildViews{
    
    [self addSubview:self.backView];
    
    [self.backView addSubview:self.headView];
    [self.headView addSubview:self.closeButton];
    [self.headView addSubview:self.titleLabel];
    
    [self.backView addSubview:self.mainView];
    [self.mainView addSubview:self.inputView];
    [self.mainView addSubview:self.forgetPassButton];
    
    
    [self setNeedsUpdateConstraints];
}

-(void)updateConstraints{
    [super updateConstraints];
    
    [self.backView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.equalTo(self);
        
    }];
    
    [self.headView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.left.right.equalTo(self.backView);
        make.height.equalTo(@44);
        
    }];
    
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.centerY.equalTo(self.headView);
        
    }];
    
    [self.closeButton mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.equalTo(self.headView);
        make.left.equalTo(self.headView).offset(8);
        make.size.mas_equalTo(CGSizeMake(15, 15));
        
    }];
    
    [self.mainView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.headView.mas_bottom);
        make.left.right.bottom.equalTo(self);
        
    }];
    
    [self.inputView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.mainView).offset(10);
        make.left.equalTo(self.mainView).offset(0);
        make.right.equalTo(self.mainView).offset(0);
        make.height.equalTo(@80);
        
    }];
    
    [self.forgetPassButton mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.inputView.mas_bottom).offset(0);
        make.right.equalTo(self.mainView).with.offset(-30);
        make.size.mas_equalTo(CGSizeMake(100, 30));
        
    }];
    
}

-(UIView *)backView{
    if (!_backView) {
        _backView = [[UIView alloc]init];
    }
    return _backView;
}

-(UIView *)headView{
    if (!_headView) {
        _headView = [[UIView alloc]init];
        
        UILabel * line = [[UILabel alloc]init];
        [_headView addSubview:line];
        line.backgroundColor = LINECOLOR;
        
        [line mas_remakeConstraints:^(MASConstraintMaker *make) {
           
            make.left.right.bottom.equalTo(_headView);
            make.height.equalTo(@0.8);
            
        }];
        
    }
    return _headView;
}

-(UIButton *)closeButton{
    if (!_closeButton) {
        _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeButton sizeToFit];
        [_closeButton setImage:[UIImage imageNamed:@"pay_close"] forState:0];
        [_closeButton setImage:[UIImage imageNamed:@"pay_close"] forState:UIControlStateSelected];
        [_closeButton addTarget:self action:@selector(closeButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _closeButton;
}

-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.textColor = BLACKCOLOR;
        _titleLabel.font = [UIFont systemFontOfSize:16];
    }
    return _titleLabel;
}

-(UIView *)mainView{
    if (!_mainView) {
        _mainView = [[UIView alloc]init];
//        _mainView.backgroundColor = [UIColor redColor];
    }
    return _mainView;
}

-(PasswordInputView *)inputView{
    if (!_inputView) {
        _inputView = [[PasswordInputView alloc]init];
//        _inputView.backgroundColor = BLACKCOLOR;
        _inputView.delegate = self;
    }
    return _inputView;
}

-(UIButton *)forgetPassButton{
    if (!_forgetPassButton) {
        _forgetPassButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_forgetPassButton setTitle:@"忘记密码？" forState:UIControlStateNormal];
        [_forgetPassButton setTitleColor:ORANGCOLOR forState:UIControlStateNormal];
        _forgetPassButton.titleLabel.font = [UIFont systemFontOfSize:13];
        [_forgetPassButton addTarget:self action:@selector(forgetPassWordAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _forgetPassButton;
}


@end
